using System;
using System.Collections.Generic;
using System.Text;

using NHibernate;
using Zwms.Data.BusinessObjects;
using Zwms.Data.Base;

namespace Zwms.Data.ManagerObjects
{
    public partial interface ISysLogNoGenManager : IManagerBase<Zwms.Data.BusinessObjects.SysLogNoGen, string>
    {
        // Get Methods
        string GetCurrentLogNo(string logKind);
    }

    partial class SysLogNoGenManager : ManagerBase<Zwms.Data.BusinessObjects.SysLogNoGen, string>, ISysLogNoGenManager
    {
        #region Constructors

        public SysLogNoGenManager()
            : base()
        {
        }
        public SysLogNoGenManager(INHibernateSession session)
            : base(session)
        {
        }

        #endregion

        #region Get Methods
        public string GetCurrentLogNo(string logKind)
        {
            ICriteria criteria = Session.GetISession().CreateCriteria(typeof(SysLogNoGen));

            criteria.Add(NHibernate.Criterion.Expression.Eq("Id", logKind));
            SysLogNoGen sysLogNoGen = (SysLogNoGen)criteria.UniqueResult();

            if (sysLogNoGen == null)
                throw new NHibernate.ObjectDeletedException("", null, null);

            int currNo = 0;
            currNo = sysLogNoGen.CurrNo;
            sysLogNoGen.CurrNo = sysLogNoGen.CurrNo + sysLogNoGen.StepLen;
            if (sysLogNoGen.CurrNo >= sysLogNoGen.MaxNo)
            {
                sysLogNoGen.CurrNo = sysLogNoGen.InitNo;
            }

            this.Update(sysLogNoGen);
            this.Session.CommitChanges();

            string sTemp = sysLogNoGen.CurrNo.ToString();
            while (sTemp.Length < sysLogNoGen.FormatLen)
            {
                sTemp = string.Format("0{0}", sTemp);
            }

            return sTemp;
        }

        #endregion
    }
}